package demo1;

public class Test8 {
    public static int binary(int[] arr, int key) {
        int left = 0;
        int right = arr.length;
        while (left <= right) {
            int mid = (left + right) >>> 1;
            if (key < arr[mid]) {
                right = mid - 1;
            }else if (key == arr[mid]) {
                return mid;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        int index = binary(arr, 5);
        System.out.println(index);
    }

}
